﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Quartz;
using shop.Model;

namespace shop.Web.PlanTask
{
    /// <summary> 处理重复订单信息 </summary>
    public class DealRepeatTradeJob:IJob
    {

        //private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();



        /// <summary> job接口 </summary>
        public async Task Execute(IJobExecutionContext context)
        {
            //
            var cmd = @"select logistics_no,spec_no, trade_no, COUNT(*) as 重复, MAX(Id) maxid, MIN(Id) id , MAX(id)-MIN(Id) 距离 INTO #t
from OutStockItem where  logistics_no<>''
group by logistics_no, spec_no, trade_no
HAVING COUNT(*)>1 AND  MAX(id)-MIN(Id)>30

-- 需要删除的 

DELETE FROM OutStockItem
FROM      OutStockItem INNER JOIN
                [#t] AS t ON OutStockItem.logistics_no = t.logistics_no AND OutStockItem.spec_no = t.spec_no AND 
                OutStockItem.Id <> t.maxid
WHERE   (ABS(OutStockItem.Id - t.maxid) > 30)

DROP TABLE #t";

            await DB.FSql.Ado.ExecuteNonQueryAsync(System.Data.CommandType.Text, cmd, null);

            cmd = @"select logistics_no,spec_no, trade_no, COUNT(*) as 重复, MAX(Id) maxid, MIN(Id) id , MAX(id)-MIN(Id) 距离 INTO #t2
from  dbo.tradeItem where  logistics_no<>''
group by logistics_no, spec_no, trade_no
HAVING COUNT(*)>1 AND  MAX(id)-MIN(Id)>30

DELETE FROM tradeItem
FROM      tradeItem INNER JOIN
                [#t2] AS t ON tradeItem.logistics_no = t.logistics_no AND tradeItem.spec_no = t.spec_no AND 
                tradeItem.id <> t.maxid
WHERE   (ABS(tradeItem.id - t.maxid) > 30)

DROP TABLE #t2";

            await DB.FSql.Ado.ExecuteNonQueryAsync(System.Data.CommandType.Text, cmd, null);



        }







    }
}
